package com.qige.homework;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

/**
 * @Author qige
 * @Date 2024-08-06 19:22
 * @Description
 */
public class homework3 {
    /*
    3.由控制台按照固定格式输入学生信息，包括学号，姓名，年龄信息，当输入的内容为exit退出；
    将输入的学生信息分别封装到一个Student对象中，再将每个Student对象加入到一个集合中，
    要求集合中的元素按照年龄大小正序排序；最后遍历集合，将集合中学生信息写入到记事本，每个学生数据占单独一行。
     */
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        while (true){
            System.out.println("请输入学生信息：");
            System.out.println("学号：");
            String id = input.next();
            System.out.println("姓名：");
            String name = input.next();
            System.out.println("年龄：");
            Integer age = input.nextInt();
            Student student = new Student(id, name, age);
            list.add(student);
            System.out.println("是否退出：输入exit退出");
            String choice = input.next();
            if ("exit".equals(choice)){
                break;
            }
        }
        list.sort(Student::compareTo);
        System.out.println(list);
        FileWriter fw = new FileWriter("day10-code/src/com/qige/homework/Student.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        for (Student student1 : list) {
            bw.write(student1.getId() + "\t" + student1.getName() + "\t" + student1.getAge() + "\n");
            bw.flush();
        }
        bw.close();
    }
}
class Student implements Comparable<Student>{
    private String id;
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;
        return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(age, student.age);
    }

    @Override
    public int hashCode() {
        int result = Objects.hashCode(id);
        result = 31 * result + Objects.hashCode(name);
        result = 31 * result + Objects.hashCode(age);
        return result;
    }

    @Override
    public int compareTo(Student o) {
        if (this.age > o.getAge()){
            return -1;
        }else if (this.age < o.getAge()){
            return 1;
        }
        return 0;
    }
}